<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace common\traits;

use common\model\base\BaseModel;

/**
 * Trait ServiceTrait
 * @package common\traits
 * @property BaseModel $model
 */
trait ServiceTrait
{
    /**
     * @var BaseModel
     */
    protected $model = null;

    /**
     * 获取列表
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        $data = $this->model->getList($param);
        return $data;
    }

    /**
     * 获取统计
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getCount($param)
    {
        $data = $this->model->getCount($param);
        return $data;
    }

    /**
     * 获取ID
     * @param $param
     * @return BaseModel|null
     */
    public function getByID($param)
    {
        $data = $this->model->getByID($param[$this->model->getPk()] ?? $param['id']);
        return $data;
    }

    /**
     * 插入数据
     * @param $param
     * @return bool
     */
    public function createData($param)
    {
        $data = $this->model->createData($param);
        return $data;
    }

    /**
     * 更新数据
     * @param $param
     * @return bool
     */
    public function updateByID($param)
    {
        $data = $this->model->updateByID($param, $param[$this->model->getPk()] ?? $param['id']);
        return $data;
    }

    /**
     * 删除数据
     * @param $param
     * @return bool
     */
    public function delByID($param)
    {
        $data = $this->model->delByID($param[$this->model->getPk()] ?? $param['id'], false);
        return $data;
    }

    /**
     * 批量删除
     * @param $param
     * @return bool
     */
    public function delByIDs($param)
    {
        $data = $this->model->delByIDs($param[$this->model->getPk().'s'] ??$param['ids'], false);
        return $data;
    }

    /**
     * 启用禁用
     * @param $param
     * @return bool
     */
    public function enableByIDs($param)
    {
        $data = $this->model->enableByIDs($param[$this->model->getPk().'s'] ??$param['ids'], $param['status'], false);
        return $data;
    }
}
